package com.hoshiicloud.user.controller.organization;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.organization.PlatOrganizationDepartmentesUsers;
import com.hoshiicloud.user.param.organization.PlatOrganizationDepartmentesUsersAddParam;
import com.hoshiicloud.user.param.organization.PlatOrganizationDepartmentesUsersQueryParam;
import com.hoshiicloud.user.param.organization.PlatOrganizationDepartmentesUsersUpdateParam;
import com.hoshiicloud.user.service.organization.PlatOrganizationDepartmentesUsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 平台机构部门人员 控制器
 * </p>
 *
 * @author campchen
 * @since 2019-05-27
 */
@Slf4j
@RestController
@RequestMapping("/organization/plat-organization-departmentes-users")
@Api(value = "租户平台机构部门与员工关联", tags = {"租户平台机构部门与员工关联"})
@ApiSort(30)
public class PlatOrganizationDepartmentesUsersController {
    @Autowired
    private PlatOrganizationDepartmentesUsersService platOrganizationDepartmentesUsersService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询平台机构部门人员
     */
    @ApiOperation(value = "分页查询平台机构部门人员")
    @PostMapping("/getPage")
    public Response page(@RequestBody PlatOrganizationDepartmentesUsersQueryParam param) {
        try {
            QueryWrapper<PlatOrganizationDepartmentesUsers> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq((param.getCompanyId() != null), PlatOrganizationDepartmentesUsers::getCompanyId, param.getCompanyId())
                    .eq((param.getPlatformId() != null), PlatOrganizationDepartmentesUsers::getPlatformId, param.getPlatformId())
                    .eq((param.getDepartmentId() != null), PlatOrganizationDepartmentesUsers::getDepartmentId, param.getDepartmentId())
                    .eq((param.getUserId() != null), PlatOrganizationDepartmentesUsers::getUserId, param.getUserId())
                    .eq(StringUtils.isNotBlank(param.getRole()), PlatOrganizationDepartmentesUsers::getRole, param.getRole())
                    .eq((param.getEnabled() != null), PlatOrganizationDepartmentesUsers::getEnabled, param.getEnabled());
            Page<PlatOrganizationDepartmentesUsers> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(platOrganizationDepartmentesUsersService.getPage(page, queryWrapper, key));
        } catch (NumberFormatException e) {
            log.error("分页查询平台机构部门人员异常", e);
            return Response.failed("分页查询平台机构部门人员异常");
        }

    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询平台机构部门人员
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询平台机构部门人员")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(platOrganizationDepartmentesUsersService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增平台机构部门人员")
    public Response save(@RequestBody @Valid PlatOrganizationDepartmentesUsersAddParam param) {
        PlatOrganizationDepartmentesUsers data;
        try {
            data = BeanUtils.covertType(param, PlatOrganizationDepartmentesUsers.class);
        } catch (Exception e) {
            log.error("新增平台机构部门人员Bean转换异常", e);
            return Response.failed("新增平台机构部门人员Bean转换异常");
        }
        return Response.data(platOrganizationDepartmentesUsersService.save(data));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改平台机构部门人员
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改平台机构部门人员")
    public Response update(@RequestBody @Valid PlatOrganizationDepartmentesUsersUpdateParam param) {
        PlatOrganizationDepartmentesUsers data;
        try {
            data = BeanUtils.covertType(param, PlatOrganizationDepartmentesUsers.class);
        } catch (Exception e) {
            log.error("修改平台机构部门人员Bean转换异常", e);
            return Response.failed("修改平台机构部门人员Bean转换异常");
        }
        return Response.data(platOrganizationDepartmentesUsersService.updateById(data));
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除平台机构部门人员
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除平台机构部门人员")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        return Response.data(platOrganizationDepartmentesUsersService.deleteById(id));
    }

    /**
     * @Author:campchen
     * @Description: StringUtils.join(ids, " ") 为缓存中添加唯一KEY  如不合适可选择更好方法
     * @Date:上午 10:18 2019/5/11
     * @Param: ids
     * @return: com.hoshiicloud.common.rpc.Response
     */
    @ApiOperation(value = "根据id集合查询平台机构部门人员")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.failed("查询平台机构部门人员id集合不能为空");
        }
        ids.sort(null);
        return Response.data(platOrganizationDepartmentesUsersService.getByIds(ids, StringUtils.join(ids, "")));
    }


}
